#include "enrollwindow.h"
#include "ui_enrollwindow.h"

enrollWindow::enrollWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::enrollWindow)
{
    ui->setupUi(this);

    setWindowFlags(Qt::FramelessWindowHint);//去除标题栏
    this->setFixedSize(800,600);
   // ui->frame->setStyleSheet("#frame{border-image: url(:/image/1.jfif);background-size: cover;}");
    //connectDB();

    //初始化密码
    eyeOpenIcon = QIcon(":/image/eye.png");
    eyecloseIcon = QIcon(":/image/close.png");
    displayPasswordAction = new QAction(eyecloseIcon,"",this);
    ui->passwordlineEdit->addAction(displayPasswordAction,QLineEdit::TrailingPosition);
    connect(displayPasswordAction,&QAction::triggered,this,&enrollWindow::togglePasswordVisibility);
    ui->passwordlineEdit->setEchoMode(QLineEdit::Password);
}

enrollWindow::~enrollWindow()
{
    delete ui;
}

void enrollWindow::initLoginWindow()
{
    ui->label_4->setStyleSheet("QTabWidget::pane { border: 0; }");
   // ui->frame->setStyleSheet("#frame{border-image: url(:/image/1.jfif);background-size: cover;");
}

void enrollWindow::connectDB()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setHostName("127.0.0.1");
    db.setDatabaseName("E:/QQQt/1/qtspcse1/20class.db");
    db.open();
    if(!db.open())
    {
        QMessageBox::critical(nullptr,"错误","数据库连接失败");
        return;
    }

}

void enrollWindow::on_closeButton_clicked()
{
    close();
}

void enrollWindow::on_enrollpushButton_clicked()
{

    //将账号密码写入数据库
    QString username = ui->usernamelineEdit->text();
    QString password = ui->passwordlineEdit->text();

    if (username.isEmpty() || password.isEmpty())
    {
        QMessageBox::warning(nullptr, "错误", "账号或密码不能为空");
        return;
    }
    //QString sql = "select *from userinfo;";
    QSqlQuery query;
    query.prepare("INSERT INTO userinfo(username,password) VALUES(:username,:password)");
    query.bindValue(":username",username);
    query.bindValue(":password",password);
    bool success =  query.exec();
    if(!success)
    {
        QMessageBox::critical(nullptr,"错误","注册失败");
        return;
    }

    QMessageBox::information(nullptr, "成功", "注册成功！");
}

void enrollWindow::togglePasswordVisibility()
{

    bool m_bVisible = false;  //隐藏、显示标志
    if (m_bVisible)  //设置为隐藏密码
    {
        ui->passwordlineEdit->setEchoMode(QLineEdit::Password);
        displayPasswordAction->setIcon( eyecloseIcon);
    }
    else  //设置为显示密码
    {
        ui->passwordlineEdit->setEchoMode(QLineEdit::Normal);
        displayPasswordAction->setIcon(eyeOpenIcon);
    }
    m_bVisible = !m_bVisible;
}


void enrollWindow::on_returnpushButton_clicked()
{
    //关闭注册页面，切换至登录界面
   emit returnloginwindow();
   close();




}
